package com.example.attempt.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

import java.io.*;

public class TextIO {

    /**
     * 读文件
     * 传入文件绝对路径，将文件内容转化为 String字符串输出
     */
    public static String readFile(String filePath) {
        String content = "";
        String strLine;
        String type = ""; //文件类型

        File file = new File(filePath);
        if (file != null && file.exists()) {
            String name = file.getName();
            type = name.substring(name.lastIndexOf(".") + 1);
        }
        try {
            //读取文件
//            System.out.println(type);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            if(type.equals("txt")){
                // 字符串拼接
                while ((strLine = bufferedReader.readLine()) != null) {
                    content += strLine;
                }
            }
            if(type.equals("docx")){
                int count=1;
                XWPFDocument xwpf = new XWPFDocument(fis);
                StringBuilder content1 = new StringBuilder();
                for (XWPFParagraph paragraph : xwpf.getParagraphs()) {
                    for (XWPFRun run : paragraph.getRuns()) {
                        if (run.getEmbeddedPictures().size() > 0) {
                            for (XWPFPicture picture : run.getEmbeddedPictures()) {
                                byte[] imageData = picture.getPictureData().getData();
                                String imageString = OCR.accurate(imageData); // 调用某个方法将图片byte数组转化为字符串
                                content1.append(imageString);
                                try{
                                    Thread.sleep(1000);
                                }catch(Exception e){
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            StringBuilder textBuilder = new StringBuilder();

                            for (CTText ctText : run.getCTR().getTList()) {
                                textBuilder.append(ctText.getStringValue());
                            }

                            String text= textBuilder.toString();
                            content1.append(text);
                        }
                    }
                }
                System.out.println("有"+count+"个ocr");
                content = content1.toString();
            }
            if(type.equals("doc")){
                WordExtractor wex = new WordExtractor(fis);
                content = wex.getText();
            }
            if(type.equals("pdf")){
                PDDocument document = PDDocument.load(file);
                PDFTextStripper pdfTextStripper = new PDFTextStripper();
                content = pdfTextStripper.getText(document);
                document.close();
            }

            // 关闭文件
            inputStreamReader.close();
            bufferedReader.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

//    public static void writeResultToFile(String origin, String compared, String path, String result) throws IOException {
//        FileWriter writer = new FileWriter(new File(path),true);
//        if (writer!=null){
//            StringBuilder buffer = new StringBuilder();
//            buffer.append("The origin text:");
//            buffer.append(origin);
//            buffer.append("\r\n");
//            buffer.append("The compared text:");
//            buffer.append(compared);
//            buffer.append("\r\n");
//            buffer.append("The compare result is: ");
//            buffer.append(result+"%");
//            buffer.append("\r\n");
//            buffer.append("\r\n");
//            writer.write(buffer.toString());
//            writer.flush();
//        }else {
//            System.out.println("文件写入失败，请检查输入的文件是否存在...");
//        }
//        writer.close();
//    }

}
